<?php
/***************************************************************
* Subs-Actions.php                                             *
*--------------------------------------------------------------*
* SMF Mod Creator                                              *
* Version 0.1 Alpha                                            *
* Written By: Labradoodle-360                                  *
*--------------------------------------------------------------*
* Copyright 2011 Matthew Kerle - All Rights Reserved           *
*--------------------------------------------------------------*
* Redistribution and use in source and binary forms, with or   *
* without modification, are permitted provided that the        *
* conditions specified in the attached                         *
* smfmodcreator_license.txt file are met.                      *
***************************************************************/
if (!defined('SMF'))
	die ('Hacking Attempt...');

/* This function actually creates the modification */
function createMod($fields = array())
{

	// Globalize everything we'll need here...
	global $context, $scripturl, $settings, $smcFunc, $boarddir, $boardurl, $modifications;

	// Come on, ladies!
	if (empty($fields))
		return;

	// Last Minute Walk-Ins!
	$fields['mod_name'] = $smcFunc['htmlspecialchars']($fields['mod_name'], ENT_QUOTES);
	$fields['identifer'] = $smcFunc['htmlspecialchars']($smcFunc['strtolower'](str_replace(' ', '_', $fields['mod_name'])));
	$fields['identifier'] = preg_replace('/[^a-zA-Z0-9-_]/', '', $fields['identifier']);

	// Possible Conflicts, here.
	if (in_array($fields['identifier'], $modifications))
	{
		$count = 1;
		$flipped_array = array_flip($modifications);
		while (isset($flipped_array[$fields['identifier'] . '_' . $count]))
		{
			$count++;
		}
		if (!isset($flipped_array[$fields['identifier'] . '_' . $count]))
			$fields['identifier'] .= '_' . $count;
	}

	// Insert Our Mod
	$smcFunc['db_insert']('insert',
		'{db_prefix}modifications',
		array(
			'mod_name' => 'string',
			'id_category' => 'int',
			'summary' => 'string',
			'author_id' => 'int',
			'submitted' => 'int',
			'identifier' => 'string',
		),
		array(
			$fields['mod_name'],
			(int) $fields['id_category'],
			$smcFunc['htmlspecialchars']($fields['summary'], ENT_QUOTES),
			(int) $fields['author_id'],
			(int) $fields['submitted'],
			$fields['identifier'],
		),
		array('id_modification')
	);

	// We need our ID for Subs-Files.php
	$modID = $smcFunc['db_insert_id']('{db_prefix}modifications', 'id_modification');

	// Our Subs-XML.php Function!
	xmlgenerate_package_info($modID, $fields);

	// Death Star; Easter Egg
	if ($fields['mod_name'] == 'Death Star')
	{
		header('Location: http://www.youtube.com/watch?v=ZbPcUrHsD8A');
	}

	// Then redirect!
	redirectexit('action=modcreator;page=view_mods;notify=created');

}

/* This function actually removes the modification */
function removeModification($modID = '')
{
	// !! We would allow deletion by {identifier} in addition to id, like we allow for accessing, -
	// !! although checkSession(); is required here, so delete URLs can't be accessed directly.

	// Globalize everything we'll need...
	global $modifications, $smcFunc, $mod_files, $boarddir;

	// Is this legit?
	if (empty($modID))
		return;
	elseif (!empty($modID) && !in_array($modID, array_keys($modifications)))
		fatal_error('Sorry! The modification could not be deleted. Please try again.', false);

	// Check our session...
	checkSession('get', '', true);

	// Sheesh, we survived!
	mod_files($modID);

	// Actually delete (it|them)!
	if (!empty($mod_files))
	{
		foreach ($mod_files as $fileID => $file)
		{
			removeFile($modID, $fileID);
		}
	}

	// Default (file|files)...
	$defaultFiles = array('index.php');
	foreach ($defaultFiles as $fID => $file)
	{
		unlink($boarddir . '/smfmodcreator/modifications/' . $modifications[$modID] . '/' . $file);
	}

	// Goodbye World!
	rmdir($boarddir . '/smfmodcreator/modifications/' . $modifications[$modID]);

	// Remove Database Row
	$smcFunc['db_query']('', '
		DELETE FROM {db_prefix}modifications
		WHERE id_modification = {int:modID}',
		array(
			'modID' => (int) $modID,
		)
	);

	// notify=removed
	redirectexit('action=modcreator;page=view_mods;notify=removed');

}

?>